package com.tsx.demo;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.tsx.utils.PdfFontUtil;

import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AddImage {
    public static void main(String[] args) {
        Path path = Paths.get(Constants.dest, "addImage.pdf");
        try (OutputStream os = Files.newOutputStream(path)) {
            PdfWriter pdfWriter = new PdfWriter(os);
            PdfDocument pdfDocument = new PdfDocument(pdfWriter);
            Document document = new Document(pdfDocument);
            // 创建一个 3 列的表格，并设置宽度自适应页面
            Table table = new Table(4); // 3列
            table.useAllAvailableWidth(); // 自动适应页面宽度
            PdfFont chinesePdfFont = PdfFontUtil.chinesePdfFont();
            // 添加表格内容
            // RGB颜色
            DeviceRgb backgroundColor = new DeviceRgb(200, 200, 200);
            Cell nameCell = new Cell().add(new Paragraph("姓名"))
                    .setFont(chinesePdfFont)
                    .setTextAlignment(TextAlignment.CENTER)
                    .setBackgroundColor(backgroundColor);
            Cell sexCell = new Cell().add(new Paragraph("性别"))
                    .setFont(chinesePdfFont)
                    .setTextAlignment(TextAlignment.CENTER)
                    .setBackgroundColor(ColorConstants.LIGHT_GRAY); // 颜色内置
            Cell idCell = new Cell().add(new Paragraph("ID"))
                    .setFont(chinesePdfFont)
                    .setTextAlignment(TextAlignment.CENTER)
                    .setBackgroundColor(ColorConstants.LIGHT_GRAY);
            Cell imageCell = new Cell().add(new Paragraph("图像"))
                    .setFont(chinesePdfFont)
                    .setTextAlignment(TextAlignment.CENTER)
                    .setBackgroundColor(ColorConstants.LIGHT_GRAY);
            table.addCell(nameCell);
            table.addCell(idCell);
            table.addCell(sexCell);
            table.addCell(imageCell);
            table.addCell(new Cell().add(new Paragraph("张三").setFont(chinesePdfFont)));
            table.addCell(new Cell().add(new Paragraph("男").setFont(chinesePdfFont)));
            table.addCell(new Cell().add(new Paragraph("1001").setFont(chinesePdfFont)));
            URL resource = AddImage.class.getClassLoader()
                    .getResource("avatar.jpg");
            assert resource != null;
            ImageData imageData = ImageDataFactory.create(resource);
            table.addCell(new Cell().add(new Image(imageData).setWidth(UnitValue.createPointValue(100))));
            document.add(table);
            document.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
